﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
using System.Data;
public partial class Role_AssignRoles : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (Context.User.Identity.IsAuthenticated)
        {
            if (!(Context.User is CustomPrincipal))
            {
                // ASP.NET's regular forms authentication picked up our cookie, but we
                // haven't replaced the default context user with our own. Let's do that
                // now. We know that the previous context.user.identity.name is the e-mail
                // address (because we forced it to be as such in the login.aspx page)				
                CustomPrincipal newUser = new CustomPrincipal(Context.User.Identity.Name);
                Context.User = newUser;
            }
        }
        if (!Context.User.Identity.IsAuthenticated)
            Response.Redirect("Login.aspx");

        if (!((CustomPrincipal)Context.User).IsInRole("SuperUser"))
            Response.Redirect("Login.aspx");

        if (Request.QueryString["id"] != null)
        {
            if (!Page.IsPostBack)
            {
                /*
                DataTable roles = Role.GetRoles().Tables[0];
                RoleList.DataSource = roles;
                RoleList.DataTextField = roles.Columns[1].ToString();
                RoleList.DataValueField = roles.Columns[0].ToString();
                RoleList.DataBind();
                */
                RoleList.Items.Clear();
                foreach (var item in RoleManager.FindAll())
                {
                    RoleList.Items.Add(new ListItem(item.RoleName, item.RoleId.ToString()));
                }
                BindData();
            }
        }
        else
            Response.Redirect("Default.aspx");
    }

    private void BindData()
		{
            
			Person user = PersonManager.FindById(int.Parse(Request.QueryString["id"]));
            RolesRepeater.DataSource = PersonManager.FindAllRoleById(int.Parse(Request.QueryString["id"]));// user.GetRoles();
			RolesRepeater.DataBind();
		}

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        base.OnInit(e);
        InitializeComponent();
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.AddRole.Click += new System.EventHandler(this.AddRole_Click);
        this.RemoveFromRole.Click += new System.EventHandler(this.RemoveFromRole_Click);
        this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    private void AddRole_Click(object sender, System.EventArgs e)
    {
        /*
        RolesBasedAthentication.User user = new User(int.Parse(Request.QueryString["id"]));
        user.AddToRole(int.Parse(RoleList.SelectedItem.Value));
        */
        BindData();
    }

    private void RemoveFromRole_Click(object sender, System.EventArgs e)
    {
        /*
        RolesBasedAthentication.User user = new User(int.Parse(Request.QueryString["id"]));
        user.RemoveFromRole(int.Parse(RoleList.SelectedItem.Value));

        BindData();
         * */
    }

}